﻿

Public Class frmMaquina


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        If txtMaquina.Text = "" Then
            MsgBox("Debe colocar un nombre valido", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        If ExisteValor("maquinas", "maquina", txtMaquina.Text) = True Then
            MsgBox("El nombre ya existe", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        Dim comando As New OleDb.OleDbCommand
        comando.CommandText = "Insert into maquinas (maquina) values ('" & txtMaquina.Text & "')"
        comando.Connection = Conexion
        comando.ExecuteNonQuery()

        PrepararTabla()
        MsgBox("Aqregado", MsgBoxStyle.Information)

    End Sub

    Private Sub frmMaquina_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PrepararTabla()
    End Sub
    Sub PrepararTabla()
        Dim data As New DataSet
        Dim adaptador As New OleDb.OleDbDataAdapter
        Dim comando As New OleDb.OleDbCommand

        comando.Connection = Conexion
        comando.CommandText = "Select * from maquinas order by maquina"
        adaptador.SelectCommand = comando
        adaptador.Fill(data, "tabla")

        'dgvMaquinas.Rows.Clear()
        dgvMaquinas.DataSource = data
        dgvMaquinas.DataMember = "tabla"
        dgvMaquinas.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dgvMaquinas.ReadOnly = True
        dgvMaquinas.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dgvMaquinas.Refresh()


    End Sub

    Private Sub dgvMaquinas_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMaquinas.CellContentClick

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim id As String
        Dim comando As New OleDb.OleDbCommand

        id = dgvMaquinas.SelectedCells(0).Value
        comando.Connection = Conexion
        comando.CommandText = "delete from maquinas where id=" & id
        comando.ExecuteNonQuery()
        MsgBox("Eliminado", MsgBoxStyle.Information)
        PrepararTabla()



    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Close()
    End Sub
End Class